/* Includes ------------------------------------------------------------------*/
#include "convert.h"
#include "math.h"
/* Public typedef ------------------------------------------------------------*/
/* Public variables ----------------------------------------------------------*/
/* Public define -------------------------------------------------------------*/
/* Public macro --------------------------------------------------------------*/
/* Public function prototypes ------------------------------------------------*/
/*******************************************************************************************

  * @brief  HexCharToByte
  * @param  hexChar: the src char
  * @retval convert to half byte 
  * @remark 

********************************************************************************************/
uint8_t HexCharToByte(char hexChar)
{
    if(hexChar >= '0' && hexChar <= '9')
    {   return (uint8_t)(hexChar - '0');  }
    else if(hexChar >= 'A' && hexChar <= 'F')
    {   return (uint8_t)(hexChar - 'A') + 10;  }
    else if(hexChar >= 'a' && hexChar <= 'f')
    {   return (uint8_t)(hexChar - 'a') + 10;  }
    return 0;
}
/*******************************************************************************************

  * @brief  HexStringToByteArray
  * @param  src, the src hex string
  *         dst, dst array to store convert result
  *         length, src string length
  * @retval 
  * @remark 

********************************************************************************************/
void HexStringToByteArray(char *src, char *dst, size_t length)
{
    uint8_t byte = 0;

    for(int i=0; i<length / 2; i++)
    {
        byte = 0;
        byte |= HexCharToByte(src[i * 2]);
        byte <<= 4;
        byte |= HexCharToByte(src[i * 2 + 1]);
        dst[i] = byte;
    }
}

/*******************************************************************************************

  * @brief  FloatIntToBin32
  * @param  decimal, the src number
  * @retval strip decimal from a double 
  * @remark 

********************************************************************************************/
uint32_t FloatIntToBin32(double decimal)
{
    uint32_t intPart = (uint32_t)(decimal);

    return intPart;
}
/*******************************************************************************************

  * @brief  FloatDecimalToBin32
  * @param  decimal, the src number
  * @retval float convert to word 
  * @remark 

********************************************************************************************/
uint32_t FloatDecimalToBin32(double decimal)
{
    uint64_t intPart = (uint64_t)(decimal);
    uint32_t result = 0;
    decimal -= intPart;

    for(int i=0; i<32; i++)
    {
        result <<= 1;

        /* clear int part */
        intPart = (uint64_t)(decimal);
        decimal -= intPart;

        /* *2, if int part is 1, then set bit 1 */
        decimal *= 2;
        intPart = (uint64_t)(decimal);

        if(intPart == 1)
        {   result |= 1;    }

        decimal -= intPart;
    }

    return result;
}
/*******************************************************************************************

  * @brief  Bin32ToDecimal
  * @param  data, the src number
  * @retval word convert to float number 
  * @remark 

********************************************************************************************/
double Bin32ToDecimal(uint32_t data)
{
    double decimal = 0;

    for(int i=31; i>=0; i--)
    {
        if((data & (1<<i)) != 0)
        {   decimal += pow(2, -(32 - i));   }
    }

    return decimal;
}
/*******************************************************************************************

  * @brief  EndianConvet
  * @param  mem, the src ponter;
  *         size, convert length
  * @retval word convert to float number 
  * @remark little endian => big endian or big endian =>little endian, use for net transmition protocol

********************************************************************************************/
void EndianConvet(void *mem , size_t size)
{
    uint8_t temp8 = 0, *data = (uint8_t *)mem;

    for(int i=0; i<(size / 2); i++)
    {
        temp8 = data[i];
        data[i] = data[size - i - 1];
        data[size - i - 1] = temp8;
    }
}
